package com.qianfeng.tiantian_banjia.utils;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpUpLoad {

	/**
	 * 涓婁紶鍥剧墖
	 * 
	 * @param 涓婁紶鐨勭綉鍧�
	 * @param filePath
	 *            鏂囦欢鎵�湪鐨勮矾寰�
	 * @return 鏈嶅姟绔繑鍥炴垚鍔熷け璐ョ殑String
	 */
	public static String upload(String url, String filePath) {
		// 鏋勫缓FileBody
		FileBody fileBody = new FileBody(new File(filePath));
		// 閲囩敤post璇锋眰鏂瑰紡鎻愪氦鏁版嵁
		FormBodyPart formBodyPart = new FormBodyPart("form", fileBody);
		// 鏀惧叆鍒板皝瑁呯殑琛ㄥ崟鐨勫疄浣撶被涓�
		MultipartEntity multipartEntity = new MultipartEntity();
		multipartEntity.addPart(formBodyPart);
		try {
			HttpClient client = new DefaultHttpClient();
			//纭畾浜唘rl
			HttpPost post = new HttpPost(url);
			//multipartEntity濉啓鍒皃ost涓�
			post.setEntity(multipartEntity);
			HttpResponse response = client.execute(post);
			if(response.getStatusLine().getStatusCode() == 200){
				return EntityUtils.toString(response.getEntity(),"utf-8");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	

}
